<?php
class ChannelStore_model extends CI_Model {
    
    public $tabName;

    public function __construct()
    {
        $this->tabName = "kfsy_channel_store.kf_channel_store";
    }

    /*
        通过药店ID和渠道ID查状态
        params
            channel_id 渠道ID
            store_id 药店ID
    */
    public function getStoreStatusById($channel_id = 0, $store_id = 0)
    {
        $where = "fk_channel_id = $channel_id AND fk_store_id = $store_id";

        $data = $this->db->where($where)->get('kfsy_channel_store.kf_channel_store')->row_array();

        return $data;
    }

    /*
        通过药店ID查状态
        params
            store_id 药店ID
    */
    public function getStoreStatusByYid($store_id = 0)
    {
        $where = "fk_store_id = $store_id";

        $data = $this->db->where($where)->get('kfsy_channel_store.kf_channel_store')->result_array();

        return $data;
    }

    /*
        通过渠道id获取药店id
        params
            chn_id 渠道id
    */
    public function getStoreIdByChnId($chn_id = 0)
    {
        $where = array();

        $where['fk_channel_id'] = intval($chn_id);

        $storeInfo = $this->db
            ->select("fk_store_id")
            ->where($where)
            ->get($this->tabName)
            ->result_array();

        $res = array();

        foreach ($storeInfo as $key => $value) {
            $res[] = $value['fk_store_id'];
        }

        return $res;
    }

    /*
        修改门店信息
        params
            chn_id 渠道id
            store_id 店铺id
            other_shop_name 第三方药店名称
            other_shop_desc 第三方药店广告
            start_time 营业开始时间
            end_time 营业结束时间
            shipping_fee 配送费
            store_status 店铺状态
    */
    public function updateChannelStore($chn_id = 0, $store_id = 0, $other_shop_name = '', $other_shop_desc = '', $start_time = '', $end_time = '', $shipping_fee = 0, $store_status = 0)
    {
        $res = false;

        if($chn_id == 1){
            $res = $this->updateMtStore($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);
        }

        if($chn_id == 2){
            $res = $this->updateBdStore($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);
        }

        if($chn_id == 3){
            $res = $this->updateJddjStore($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);
        }

        if($chn_id == 4){
            $res = $this->updateElemeStore($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);
        }

        return $res;
        
    }

    /*
        修改美团门店信息
        params
            chn_id 渠道id
            store_id 店铺id
            other_shop_name 第三方药店名称
            other_shop_desc 第三方药店广告
            start_time 营业开始时间
            end_time 营业结束时间
            shipping_fee 配送费
            store_status 店铺状态
            
    */
    public function updateMtStore($chn_id = 0, $store_id = 0, $other_shop_name = '', $other_shop_desc = '', $start_time = '', $end_time = '', $shipping_fee = 0, $store_status = 0)
    {
        $this->load->library('AdminApi/MtApi','','MtApi');
        $MtApi = $this->MtApi;
        
        $res = $MtApi->updateStoreInfo($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);

        return $res;
    }

    /*
        修改百度门店信息
        params
            chn_id 渠道id
            store_id 店铺id
            other_shop_name 第三方药店名称
            other_shop_desc 第三方药店广告
            start_time 营业开始时间
            end_time 营业结束时间
            shipping_fee 配送费
            store_status 店铺状态
            
    */
    public function updateBdStore($chn_id = 0, $store_id = 0, $other_shop_name = '', $other_shop_desc = '', $start_time = '', $end_time = '', $shipping_fee = 0, $store_status = 0)
    {
        $this->load->library('AdminApi/MtApi','','MtApi');
        $MtApi = $this->MtApi;
        
        $res = $MtApi->updateStoreInfo($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);

        return $res;
    }

    /*
        修改京东门店信息
        params
            chn_id 渠道id
            store_id 店铺id
            other_shop_name 第三方药店名称
            other_shop_desc 第三方药店广告
            start_time 营业开始时间
            end_time 营业结束时间
            shipping_fee 配送费
            store_status 店铺状态
            
    */
    public function updateJddjStore($chn_id = 0, $store_id = 0, $other_shop_name = '', $other_shop_desc = '', $start_time = '', $end_time = '', $shipping_fee = 0, $store_status = 0)
    {
        $this->load->library('AdminApi/JddjApi','','JddjApi');
        $JddjApi = $this->JddjApi;
        
        $res = $JddjApi->updateStoreInfo($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);

        return $res;
    }

    /*
        修改饿了么门店信息
        params
            chn_id 渠道id
            store_id 店铺id
            other_shop_name 第三方药店名称
            other_shop_desc 第三方药店广告
            start_time 营业开始时间
            end_time 营业结束时间
            shipping_fee 配送费
            store_status 店铺状态
            
    */
    public function updateElemeStore($chn_id = 0, $store_id = 0, $other_shop_name = '', $other_shop_desc = '', $start_time = '', $end_time = '', $shipping_fee = 0, $store_status = 0)
    {
        $this->load->library('AdminApi/ElemeApi','','ElemeApi');
        $ElemeApi = $this->ElemeApi;
        
        $res = $ElemeApi->updateStoreInfo($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);

        return $res;
    }
}